﻿using System;
using System.Windows.Forms;
using 售后客户管理.ModelLibrary;

namespace 售后客户管理.Forms
{

    public partial class FmWorkPlan : Form
    {
        public FmWorkPlan()
        {
            InitializeComponent();
        }
        public FmWorkPlan(WorkPlan workPlan, System.Data.SqlClient.SqlConnection sqlConnection)
        {
            InitializeComponent();
            SqlConnection = sqlConnection;
            User = workPlan.userName;
            if (workPlan.planID != "")
            {
                ObjectToApp(workPlan);
            }

        }
        private string User = "";
        private System.Data.SqlClient.SqlConnection SqlConnection = null;


        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.rtbTaskDescription.Text == "")
            {
                return;
            }

            WorkPlan workPlan = AppToObject();
            if (SqlClassLibrary.SqlGetSet.SetWorkPlan(workPlan, SqlConnection) > 0)
                MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK);

            this.Close();
            this.DialogResult = DialogResult.OK;
        }

        private void FmWorkPlan_Load(object sender, EventArgs e)
        {
            if (SqlConnection != null)
            {
                this.cbbAssistanceDepartment.DataSource = SqlClassLibrary.SqlGetSet.GetDictList("AssistanceDepartment", SqlConnection);
                this.cbbAssistanceStaff.DataSource = SqlClassLibrary.SqlGetSet.GetDictList("AssistanceStaff", SqlConnection);
                this.cbbOwnerType.DataSource = SqlClassLibrary.SqlGetSet.GetDictList("OwnerType", SqlConnection);
                this.cbbPlanType.DataSource = SqlClassLibrary.SqlGetSet.GetDictList("PlanType", SqlConnection);
                this.cbbPrincipal.DataSource = SqlClassLibrary.SqlGetSet.GetDictList("Principal", SqlConnection);
                this.cbbTaskType.DataSource = SqlClassLibrary.SqlGetSet.GetDictList("TaskType", SqlConnection);
            }
            this.btnSave.Paint += DrawingImage.RoundButton_Paint;
            this.btnQuit.Paint += DrawingImage.RoundButton_Paint;
            this.BackColor = Common.MyColor.NewColor_Lavender;
            this.tbUserName.Text = User;
            this.panel2.BackColor = Common.MyColor.NewColor_LightLavender;
        }

        private void ObjectToApp(WorkPlan workPlan)
        {
            this.tbPlanID.Text = workPlan.planID;
            this.tbUserName.Text = workPlan.userName;
            this.dtpActualCompleteDate.Text = workPlan.actualCompleteDate;
            this.cbbAssistanceDepartment.Text = workPlan.assistanceDepartment;
            this.cbbAssistanceStaff.Text = workPlan.assistanceStaff;
            this.tbBeiZhu.Text = workPlan.beiZhu;
            this.cbbOwnerType.Text = workPlan.ownerType;
            this.dtpPlanCompleteDate.Text = workPlan.planCompleteDate;
            this.dtpPlanStartDate.Text = workPlan.planStartDate;
            this.cbbPlanType.Text = workPlan.planType;
            this.cbbPrincipal.Text = workPlan.principal;
            this.tbProgressRate.Text = workPlan.processRate.ToString();
            this.rtbTaskDescription.Text = workPlan.taskDescription;
            this.cbbTaskType.Text = workPlan.taskType;
            this.dtpPlanDate.Text = workPlan.planDate;
            this.tbWorkload.Text = workPlan.workLoad.ToString();
        }

        private WorkPlan AppToObject()
        {
            WorkPlan workPlan = new WorkPlan();
            workPlan.planID = this.tbPlanID.Text;
            workPlan.userName = this.tbUserName.Text;
            workPlan.actualCompleteDate = this.dtpActualCompleteDate.Text;
            workPlan.assistanceDepartment = this.cbbAssistanceDepartment.Text;
            workPlan.assistanceStaff = this.cbbAssistanceStaff.Text;
            workPlan.beiZhu = this.tbBeiZhu.Text;
            workPlan.ownerType = this.cbbOwnerType.Text;
            workPlan.planCompleteDate = this.dtpPlanCompleteDate.Text;
            workPlan.planStartDate = this.dtpPlanStartDate.Text;
            workPlan.planType = this.cbbPlanType.Text;
            workPlan.principal = this.cbbPrincipal.Text;
            workPlan.processRate = this.tbProgressRate.Text;
            workPlan.taskDescription = this.rtbTaskDescription.Text;
            workPlan.taskType = this.cbbTaskType.Text;
            workPlan.workLoad = this.tbWorkload.Text;
            workPlan.planDate = this.dtpPlanDate.Text;
            workPlan.yearMonthWeek = "";
            return workPlan;
        }

        private void dtpPlanStartDate_ValueChanged(object sender, EventArgs e)
        {
            if (this.dtpPlanStartDate.Text == "")
                return;
            DateTime dateTimeStart = Convert.ToDateTime(this.dtpPlanStartDate.Text);
            if (dateTimeStart > DateTime.Now)
            {
                this.dtpPlanCompleteDate.Text = dateTimeStart.ToShortDateString();
                this.dtpActualCompleteDate.Text = dateTimeStart.ToShortDateString();
            }
        }

        private void FmWorkPlan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S && e.Control)
            {
                btnSave_Click(btnSave, new EventArgs()); ;
            }
        }
    }
}
